package com.thread.comm;
class DBTools{
	private volatile boolean prevIsA = false;
	public synchronized void backupA(){
		try {
			while(prevIsA==true){
				wait();
			}
			for(int i=0; i<5; i++){
				System.out.println("AAAAA");
			}
			prevIsA = true;
			notifyAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public synchronized void backupB(){
		try {
			while(prevIsA==false){
				wait();
			}
			for(int i=0; i<5; i++){
				System.out.println("BBBBB");
			}
			prevIsA = false;
			notifyAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class BackA extends Thread{
	private DBTools dbTools;
	public BackA(DBTools dbTools){
		this.dbTools = dbTools;
	}
	public void run(){
		dbTools.backupA();
	}
}
class BackB extends Thread{
	private DBTools dbTools;
	public BackB(DBTools dbTools){
		this.dbTools = dbTools;
	}
	public void run(){
		dbTools.backupB();
	}
}
public class Run {

	public static void main(String[] args) {
		DBTools dbTools = new DBTools();
		for(int i=0;i<20; i++){
			BackB output = new BackB(dbTools);
			output.start();
			BackA input=new BackA(dbTools);
			input.start();
		}

	}

}
